<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <el-dialog title="添加账户" :visible.sync="updatedialogFormVisible">
        <el-form :model="adminInfo" status-icon :rules="rules" ref="adminInfo" label-width="100px"
                 class="demo-adminInfo">
            <el-form-item label="请输入账号" prop="username">
                <el-input v-model="adminInfo.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请输入密码" prop="password">
                <el-input type="password" v-model="adminInfo.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPassWord">
                <el-input type="password" v-model="adminInfo.checkPassWord" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新用户状态" prop="state">
                <el-select v-model="adminInfo.state" placeholder="请选择新用户状态">
                    <el-option label="0:打开" value="0"></el-option>
                    <el-option label="1:关闭" value="1"></el-option>
                    <el-option label="2:冻结" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="用户编号" prop="eid">
                <el-select v-model="adminInfo.eid" placeholder="请选择用户编号">
                    <el-option v-for="(e,i) in InfoArr" :label="e.id" :value="e.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('adminInfo')">提交</el-button>
                <el-button @click="resetForm('adminInfo')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
<script>
    $.get("/ssm/Info/list",function (bakeData) {
        danghui.InfoArr = bakeData.data;
    });
    var danghui = new Vue({
        el: "#app",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入'));
                } else {
                    if (danghui.adminInfo.checkPassWord !== '') {
                        danghui.$refs.adminInfo.validateField('checkPassWord');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== danghui.adminInfo.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                InfoArr:[],
                updatedialogFormVisible: true,
                adminInfo: {},
                rules: {
                    username: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPassWord: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    state: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    eid:[
                        {validator: validatePass, trigger: 'blur'}
                    ]
                }
            };
        },

        methods: {

            submitForm(formName) {
                danghui.$refs[formName].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/Info/addAccount", danghui.adminInfo, function (backData) {
                            if (backData.code == 2) {
                                danghui.$message.error(backData.msg);
                            } else {
                                danghui.updatedialogFormVisible = false;
                                danghui.$message.success("添加成功");
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                danghui.$refs[formName].resetFields();
            }
        },
    });
</script>
</body>
</html>